/**
 * 
 */
var time;
Ext.Ajax.request({
			url : 'taskAction!queryTime.action',
			success : function(response) {
				var res = response.responseText;
				res = Ext.JSON.decode(res);
				var currentTime = res.currentTime;
				time = currentTime;
			}
		});
Ext.define('Task', {
			extend : 'Ext.data.Model',
			fields : [{
						name : 'id'
					}, {
						name : 'recid'
					}, {
						name : 'recname'
					}, {
						name : 'senderid'
					}, {
						name : 'sendername'
					}, {
						name : 'title'
					}, {
						name : 'content'
					}, {
						name : 'status'
					}, {
						name : 'plantime'
					}, {
						name : 'type'
					}, {
						name : 'finishtime'
					}, {
						name : 'finishcontent'
					}, {
						name : 'isread'
					}, {
						name : 'sendername'
					}, {
						name : 'senderdept'
					}]
		});
var store = Ext.create('Ext.data.Store', {
			model : 'Task',
			pageSize : 20,
			proxy : {
				type : 'ajax',
				url : 'taskAction!queryPage.action',
				reader : {
					type : 'json',
					root : 'pageList'
				}
			}
		});
String.prototype.replaceAll = function(s1, s2) {
	return this.replace(new RegExp(s1, "gm"), s2);
}
function readTitle(data, cell, record, rowIndex, columnIndex, store) {
	var id = record.get('id');
	var isread = record.get('isread'), status = record.get('status');
	var title = data, color;
	var plantime = record.get('plantime').replaceAll('-', ''), _time;
	if (isread == 0) {
		color = 'color:#9932CC;';
	} else if (isread == 3) {
		color = 'color:#666666;';
	} else {
		color = 'color:#15428b;';
	}
	if (plantime != null) {
		plantime = plantime.replaceAll('-', '');
		_time = time.substr(0, 10).replaceAll('-', '');
		if (plantime - _time <= 3 && status == 2) {
			color = 'color:red;';
		}
	}
	var style = color
			+ 'font-size: 13px;cursor: pointer;font-weight:bold;text-decoration: underline;font-family:楷体';
	title = '<span style="' + style + '"  data="' + title
			+ '" onclick="openDetail(' + id + ',' + isread + ')">' + title
			+ '</span>';
	return title;
}
function openDetail(id, isread) {
	if (isread == 0) {
		Ext.Ajax.request({
					url : 'taskAction!read.action?id=' + id + '&role=quxian',
					success : function(response) {
						Ext.util.Cookies.set('taskId', id);
						Ext.util.Cookies.set('flag', 'look');
						Ext.util.Cookies.set('role', 'quxian');
						window.top.Utils.addPage('taskQuery' + id, '任务详情',
								'js/resources/business/task/taskFeedback.js');
					},
					failure : function(response, opts) {
						console.log('server-side failure with status code '
								+ response.status);
					}
				});

	} else {
		Ext.util.Cookies.set('taskId', id);
		Ext.util.Cookies.set('flag', 'look');
		Ext.util.Cookies.set('role', 'quxian');
		window.top.Utils.addPage('taskQuery' + id, '任务详情',
				'js/resources/business/task/taskFeedback.js');
	}
}
/*
 * var addBtn = { xtype : 'button', text : '新建任务', name : 'add', iconCls :
 * 'icon-add', // hidden : true, handler : function() {
 * window.top.Utils.addPage('taskAdd', '任务新建',
 * 'js/resources/business/task/taskAdd.js'); } }; var sendBtn = { xtype :
 * 'button', text : '发布任务', name : 'send', iconCls : 'icon-rrss_go', // hidden :
 * true, handler : function() { } }; var editBtn = { xtype : 'button', text :
 * '修改', name : 'edit', iconCls : 'icon-edit', // hidden : true, handler :
 * function() { var selMode = grid.getSelectionModel(); var model =
 * selMode.getSelection(); if (model.length == 1) { var raw = model[0].raw; if
 * (raw.status == 1) { Ext.util.Cookies.set('taskId', raw.id);
 * Ext.util.Cookies.set('flag', 'edit'); window.top.Utils.addPage('taskEdit' +
 * raw.id, '任务修改', 'js/resources/business/task/taskAdd.js'); } else {
 * Ext.Msg.alert('提示', '不能修改非草稿信息！'); return; } } else if (model.length > 1) {
 * Ext.Msg.alert('提示', '只能选择一条修改！'); return; } else { Ext.Msg.alert('提示',
 * '请选择要修改的数据行！'); return; } } };
 */
var feedbackBtn = {
	xtype : 'button',
	text : '反馈',
	name : 'feedback',
	iconCls : 'icon-rrss_go',
	// hidden : true,
	handler : function() {
		var selMode = grid.getSelectionModel();
		var model = selMode.getSelection();
		if (model.length == 1) {
			var raw = model[0].raw;
			if (raw.status != 2) {
				Ext.Msg.alert('提示', '该记录无需反馈!');
				return false;
			}
			Ext.util.Cookies.set('taskId', raw.id);
			Ext.util.Cookies.set('flag', 'feedback');
			window.top.Utils.addPage('taskFeedback' + raw.id, '任务反馈',
					'js/resources/business/task/taskFeedback.js');
		} else {
			Ext.Msg.alert('提示', '请选择一条反馈！');
			return;
		}
	}
};
var deleteBtn = {
	xtype : 'button',
	text : '删除',
	name : 'delete',
	width : 50,
	iconCls : 'icon-delete',
	// hidden : true,
	handler : function() {
		var selMode = grid.getSelectionModel();
		var model = selMode.getSelection();
		if (model[0]) {
			var ids = [];// 删除多个
			Ext.Array.each(model, function(item) {
						if (item.raw.status != 1) {
							Ext.Msg.alert('提示', '您选择的数据中存在非草稿状态数据，请重新选择！');
							ids = [];
							return false;
						} else {
							ids.push(item.raw.id);
						}
					});
			if (ids.length == 0) {
				return;
			} else {
				Ext.Msg.show({
							title : '确认删除',
							buttons : Ext.MessageBox.OKCANCEL,
							msg : '确定删除选择项?',
							fn : function(btn) {
								if (btn == 'ok') {
									Ext.Ajax.request({
												url : 'taskAction!delete.action?ids='
														+ ids,
												success : function(response) {
													Ext.Msg.show({
																title : '提示',
																msg : '删除成功！',
																buttons : Ext.Msg.OK,
																width : 300,
																fn : function() {
																	store
																			.reload();
																}
															});
												},
												failure : function(response,
														opts) {
												}
											});
								}
							}
						});
			}
		}
	}
};

var cols = [{
			text : 'id',
			sortable : true,
			hidden : true,
			dataIndex : 'id'
		}, {
			renderer : function(value, metadata, record, rowIndex, colIndex,
					store) {
				if (record.get('isread') == 0) {
					return "<img src='images/task/task_new.png'/>";
				} else {
					return "<img src='images/task/task_read.png'/>";
				}
			},
			width : 28
		}, {
			text : '任务主题',
			sortable : true,
			width : 200,
			renderer : readTitle,
			dataIndex : 'title'
		}, {
			text : '紧急程度',
			sortable : true,
			dataIndex : 'type',
			renderer : function(v) {
				if (v == 1) {
					return '重要紧急';
				}
				if (v == 2) {
					return '重要不紧急';
				}
				if (v == 3) {
					return '紧急不重要';
				}
				if (v == 4) {
					return '不重要不紧急';
				}
			}
		}, {
			text : '任务发送单位',
			sortable : true,
			width : 200,
			dataIndex : 'senderdept'
		}, {
			text : '发送人',
			sortable : true,
			width : 200,
			dataIndex : 'sendername'
		}, {
			text : '计划完成时间',
			sortable : true,
//			renderer : Ext.util.Format.dateRenderer('Y-m-d'),
			dataIndex : 'plantime'
		}, {
			text : '任务状态',
			sortable : true,
			dataIndex : 'status',
			renderer : function(v) {
				if (v == 1) {
					return '草稿';
				}
				if (v == 2) {
					return '待反馈';
				}
				if (v == 3) {
					return '已反馈';
				}
				if (v == 4) {
					return '已删除';
				}
			}
		}, {
			text : '实际完成时间',
			sortable : true,
//			renderer : Ext.util.Format.dateRenderer('Y-m-d'),
			dataIndex : 'finishtime'
		}];
var grid = Ext.create('Ext.grid.Panel', {
			enableOverflow : true,
			store : store,
			border : 0,
			selModel : Ext.create('Ext.selection.CheckboxModel'),
			columns : cols,
			stripeRows : false,
			tbar : [feedbackBtn, '->', {
						width : 350,
						fieldLabel : '搜索',
						labelWidth : 50,
						emptyText : '请输入任务主题',
						xtype : 'searchfield',
						store : store
					}],
			viewConfig : {
				forceFit : true
				// 让grid的列自动填满grid的整个宽度，不用一列一列的设定宽度。
			},

			bbar : Ext.create('Ext.PagingToolbar', {
						pageSize : 20,// 每页20条
						store : store,
						displayInfo : true,
						plugins : Ext.create('Ext.ux.ProgressBarPager', {})
					})
		});
function ExternalRefresh() {
	store.reload();
};
Ext.onReady(function() {
			Ext.create('Ext.container.Viewport', {
						layout : 'fit',
						items : [grid]
					});
			store.load();
		});
